<template>
  <el-drawer v-model="drawer" @closed="closed">
    <!-- 标题部分 -->
    <template #header>
      <h4>{{ edit ? $t('operation.edit'): $t('operation.add') }}</h4>
    </template>
    <template #default>
      <el-form
        ref="form"
        :model="formParams"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item :label="$t('boat.bname')" prop="brandName">
          <el-input placeh:older="$t('boat.bname')" v-model="formParams.brandName"></el-input>
        </el-form-item>
        <el-form-item :label="$t('boat.bdes')">
          <el-input
            type="textarea"
            :placeholder="$t('boat.bdes')"
            v-model="formParams.brandDepict"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('boat.blogo')" prop="brandLogoUrl">
          <pic-upload v-model="formParams.brandLogoUrl" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            size="default"
            @click="save"
          >
            保存
          </el-button>
          <el-button type="primary" size="default" @click="drawer = false">
            取消
          </el-button>
        </el-form-item>
      </el-form>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { brandAdd, brandEdit } from '@/api/boat/brand'
import { roleGetAll } from '@/api/system/role'
import { ElMessage } from 'element-plus'

let roles = []
onMounted(async () => {
  let { status, data } = await roleGetAll()

  if (status == 200) {
    roles = data
  }
})

let statusArr = ref<any>(['正常', '冻结'])

const defaultForm = {
  id: '',
  brandName: '',
  brandDepict: '',
  brandLogoUrl: '',
}
let formParams = reactive<any>({
  ...defaultForm,
})
const rules = {
  brandName: [{ required: true, message: '请输入', trigger: 'blur' }],
  brandDepict: [{ required: true, message: '请输入', trigger: 'blur' }],
  brandLogoUrl: [{ required: true, message: '请上传', trigger: 'blur' }],
}

let drawer = ref(false)
let edit = ref(false)
const show = (row) => {
  if (row) {
    edit.value = true
    Object.keys(formParams).forEach((key) => {
      formParams[key] = row[key]
    })
  } else {
    edit.value = false
  }
  drawer.value = true
}

let loading = ref(false)
let form = ref()
//保存按钮的方法
const save = async () => {
  await form.value.validate()
  loading.value = true
  let result: any
  let param = { ...formParams }

  if (edit.value) {
    result = await brandEdit(param)
  } else {
    result = await brandAdd(param)
  }
  loading.value = false
  if (result.status == 200) {
    ElMessage({
      type: 'success',
      message: '操作成功',
    })
    drawer.value = false
    $emit('callback')
  } else {
    ElMessage({
      type: 'error',
      message: '操作失败',
    })
  }
}

const closed = () => {
  Object.assign(formParams, defaultForm)
}

//组件内部数据对外关闭的，别人不能访问
//如果想让外部访问需要通过defineExpose方法对外暴露
defineExpose({
  show,
})
//自定义事件的方法
let $emit = defineEmits(['callback'])
</script>
